package tgc.edu.exam.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.entity.BookRecord;
import tgc.edu.exam.entity.BookType;
import tgc.edu.exam.entity.Books;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.User;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.BookRecordService;
import tgc.edu.exam.service.BookService;
import tgc.edu.exam.service.BookTypeService;
import tgc.edu.exam.service.UserService;

@Controller
@RequestMapping("/userbook")
public class UserbookController {

	@Autowired
	private BookService bookService;
	@Autowired
	private BookTypeService bookTypeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private BookRecordService bookRecordService;
	@Autowired
	private UserService userService;
	/**
	 * 种类
	 * @param map
	 * @return
	 */
	@RequestMapping("/books")
	public String books(ModelMap map) {
		List<BookType> type = bookTypeService.findAll();
		map.put("type", type);
		return "userbook/books";
	}
	/**
	 * 按种类分书
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/booktype")
	public String booktype(Integer id,ModelMap map) {
		List<Books> books = bookService.findByTypeId(id);
		map.put("books", books);
		return "userbook/booktype";
		
	}
	
	/**
	 * 借书
	 * @param id
	 * @return
	 */
	@RequestMapping("/jie")
	@ResponseBody
	public String jie(Integer id) {
		try {
			SysUser user = userUtils.getUser();
			BookRecord record = new BookRecord();
			record.setUsername(user.getUsername());
			record.setName(user.getName());
			User user2 = userService.findById(user.getId());
			record.setPhone(user2.getPhone());
			Books book = bookService.findById(id);
			Integer number = book.getNumber()-1;
			book.setNumber(number);
			bookService.save(book);
			record.setTitle(book.getTitle());
			record.setByid(book.getByid());
			record.setSuthor(book.getSuthor());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date = df.format(new Date());
			record.setBorrowData(date);
			
			Date dtime = new Date();
			Date dt = stepDay(dtime , 60);
			record.setReturnData(df.format(dt));
			record.setState("等待管理员审核");
			bookRecordService.save(record);
			return "请等待管理员审核！";
		}catch(Exception e) {
			return "借书失败";
		}
		
	}
	
	/**
	 * 获取后60天的日期
	 * @param dtime
	 * @param day
	 * @return
	 */
	private Date stepDay(Date dtime, int day) {
		Calendar instance = Calendar.getInstance();
		instance.setTime(dtime);
		//获取天数结束的日期
		instance.add(Calendar.DATE, day);
		return instance.getTime();
	}
	/**
	 * 我的借书
	 * @param map
	 * @return
	 */
	@RequestMapping("/borrow")
	public String borrow(ModelMap map) {
		SysUser user = userUtils.getUser();
		List<BookRecord> model = bookRecordService.findByUsername(user.getUsername());
		map.put("model", model);
		return "userbook/borrow";
	}
	/**
	 * 还书
	 * @param id
	 * @return
	 */
	@RequestMapping("/returnbook")
	@ResponseBody
	public String returnbook(Integer id) {
		BookRecord record = bookRecordService.findById(id);
		Books book = bookService.findByByid(record.getByid());
		Integer number = book.getNumber()+1;
		book.setNumber(number);
		bookService.save(book);
		record.setState("已还");
		bookRecordService.save(record);
		return "还书成功";
		
	}
	@RequestMapping("/message")
	public String message(ModelMap map) {
		SysUser user = userUtils.getUser();
		map.put("user", user);
		return "userbook/message";
		
	}
	@RequestMapping("/changepwd")
	public String changepwd(ModelMap map) {
		return "userbook/changepwd";
		
	}
	/*
	 * 读者违约处理页面
	 */
	@RequestMapping("/breach")
	public String breach(ModelMap map){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = df.format(new Date());
		List<BookRecord> findAll = bookRecordService.findByReturnDataLessThanAndState(date,"已借");
		map.put("findAll", findAll);
		return "userbook/breach";
	}
	/*
	 * 读者违约解除限制
	 */
	@RequestMapping("/relieve")
	@ResponseBody
	public Object relieve(Integer id){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		BookRecord record = bookRecordService.findById(id);
		String endDate = df.format(new Date());
		Calendar instance = Calendar.getInstance();
		Date endDate2;
		try {
			endDate2 = df.parse(endDate);
			instance.setTime(endDate2);
			instance.add(Calendar.DATE, 3);
			String format2 = df.format(instance.getTime());
			record.setReturnData(format2);
			bookRecordService.save(record);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "成功加了3天时间";
	}
	
}
